﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace GameCore
{
    /// <summary>
    /// 游戏物体扩展
    /// </summary>
    public static class GameObjectExtension
    {

        /// <summary>
        /// 获取游戏组件如果没有就添加该组件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="gameObject"></param>
        /// <returns></returns>
        public static T GetAndAddComponent<T>(this GameObject gameObject) where T : Component
        {
            if (gameObject == null)
                return default(T);
            T component = gameObject.GetComponent<T>();
            if (component == null) 
            {
                component = gameObject.AddComponent<T>();
            }
            return component;
        }
    }
}
